import base from './base.js'

const host = `${base.host}/api`
export default {
    request(data) {
        data.url = `${host}/${data.url}`;
        data.header = {
            Accept: `application/json`,
        }
        //传递验证令牌
        const token = uni.getStorageSync('token');
        if (token) {
            data.header.Authorization = `Bearer ${token}`
        }

        return uni.request(data).then(result => {
            if (undefined == result.statusCode) {
                return
            }
            if (result.statusCode >= 200 && result.statusCode < 300) {
                if (result.data.message && result.data.message !== '') {
                    uni.showToast({
                        icon: 'none',
                        title: result.data.message,
                    })
                }
                return Promise.resolve(result.data)
            }

            if (result.statusCode == 422) {
                let content = Object.entries(result.data.errors).map(([key, e]) => e.join('\n')).join(
                    '\r\n');
                uni.showModal({
                    title: '提交数据错误',
                    content,
                    showCancel: false,
                    confirmText: '确定'
                })
                return Promise.reject();
            }

            if (result.statusCode == 401) {
                uni.clearStorage()
                uni.showToast({
                    icon: "none",
                    title: "尚未登录"
                })
                uni.reLaunch({
                    url: '/subpage/user/login'
                })
            }
            if (result.statusCode == 403) {
                uni.showToast({
                    icon: "none",
                    title: result.data.message
                })
            }
			if (result.statusCode == 404) {
			    uni.showToast({
			        icon: "none",
			        title: result.data.message
			    })
			}

            if (result.statusCode >= 500) {
                uni.showToast({
                    icon: "none",
                    title: result.data.error
                })
            }

            return Promise.reject(data)
        })
    },
    get(url, data = {}, options = {}) {
        return this.request({
            url,
            data,
            ...options
        })
    },
    post(url, data = {}, options = {}) {
        return this.request({
            url,
            data,
            method: 'POST',
            ...options
        })
    },
    put(url, data = {}, options = {}) {
        return this.request({
            url,
            data,
            method: 'PUT',
            ...options
        })
    },
    delete(url) {
        return this.request({
            url,
            method: 'DELETE'
        })
    },
    upload(url, filePath) {

        return new Promise(async (resolve, reject) => {
            //传递验证令牌
            const token = uni.getStorageSync('token');

            try {
                uni.uploadFile({
                    header: {
                        Authorization: `Bearer ${token}`
                    },
                    url: url, //仅为示例，非真实的接口地址
                    filePath: filePath,
                    name: 'file',
                    success: (uploadFileRes) => {
                        if (uploadFileRes.data != "") {
                            resolve(JSON.parse(uploadFileRes.data))
                        } else {
                            uni.showToast({
                                icon: "error",
                                title: "文件上传错误"
                            })
                            reject("文件错误")
                        }
                    },
                    fail(e) {
                        console.log("fail错误")
                        reject(e)
                    }
                })
            } catch (e) {
                //TODO handle the exception
                console.log(e)
            }

        })
    }

}
